package com.smartao.base.selectitem;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartao.core.dao.BaseEntityDao;

@Repository
public class SelectitemDaoImpl extends BaseEntityDao<Selectitem, Long> implements SelectitemDao{

	@Override
	public Selectitem findByCode(String code) {
		return findUnique(Restrictions.eq("code", code));
	}

	@Override
	public List<Selectitem> findBySelecttype(Long selecttypeId) {
		return find(Restrictions.eq("selecttype.id", selecttypeId), Restrictions.isNull("parent"));
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Selectitem> findBySelecttypeCode(String code) {
		// TODO: 发现这样写return find(Restrictions.eq("selecttype.code",code))居然不行;
		String hql = "FROM  Selectitem WHERE selecttype.code = :code";
		return getSession().createQuery(hql).setString("code", code).list();
	}

	@Override
	public void deleteByselecttypeId(Long selecttypeId) {
		String hql = "DELETE From Selectitem where selecttype.id=:selecttypeId";
		getSession().createQuery(hql)
			.setLong("selecttypeId", selecttypeId)
			.executeUpdate();
	}

	@Override
	public Long countBySelecttype(Long selecttypeId) {
		String hql = "SELECT COUNT(*) FROM Selectitem where selecttype.id=?";
		return findUnique(hql, selecttypeId);
	}

	@Override
	public List<Selectitem> findByParent(Long parentSelectitemId) {
		return findBy("parent.id", parentSelectitemId);
	}
}
 